package com.china08.yxyapi.api.manage.tec.teaching.continuedu.mytask;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserFavorite;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserFavoriteRepository;

/**
 * 
 * @annotation: 继续教育-我的收藏-列表
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyMyCollectionSchController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduUserFavoriteRepository yxyContinueduUserFavoriteRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/mycollect/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        Page<YxyContinueduUserFavorite> page = null;
        String userId = getProfile(request, response).getId();
        String keyword = reqModel.keyword;

        if (StringUtils.isBlank(keyword)) {
            page = yxyContinueduUserFavoriteRepository.findByUserId(userId, pageable);
        } else {
            page = yxyContinueduUserFavoriteRepository.findByUserIdAndLessonNameContaining(userId, keyword, pageable);
        }
        List<RespModel> list = new ArrayList<YxyMyCollectionSchController.RespModel>();
        if (page != null && page.getContent().size() > 0) {
            for (YxyContinueduUserFavorite yxyContinueduUserFavorite : page) {
                RespModel model = new RespModel();
                model.courseCount = yxyContinueduUserFavorite.getCount();
                model.lessonId = yxyContinueduUserFavorite.getLessonId();
                model.name = yxyContinueduUserFavorite.getLessonName();
                model.updDate = yxyContinueduUserFavorite.getUploadDate();
                model.uploadUser = yxyContinueduUserFavorite.getUploadUserName();
                model.cover = yxyContinueduUserFavorite.getCover();
                list.add(model);
            }
        }
        Page<RespModel> pageRes = new PageImpl<RespModel>(list, pageable, page.getTotalElements());

        return pageRes;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 搜索
        public String keyword;

    }

    protected static class RespModel extends YxyApiRespModel {
        // 课程ID
        public String lessonId;
        // 课程名称
        public String name;
        // 贡献者
        public String uploadUser;
        // 课程数
        public Integer courseCount;
        // 更新时间
        public String updDate;
        // 封面
        public String cover;

    }
}
